package com.hwua.dao.impl;

import com.hwua.dao.CourseDao;
import com.hwua.entity.Course;
import com.hwua.entity.Teacher;
import com.hwua.util.DBHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author CQY
 * 实现课程接口
 */
public class CourseDaoImpl implements CourseDao{
    @Override
    public int save(Course course) {
        String sql = "insert into course(courseid,coursename,teaid) values(?,?,?)";
        Object[] params = {course.getCourseId(),course.getCourseName(),course.getTeaId()};
        int row = 0;
        try {
            row = DBHelper.update(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }

    @Override
    public int update(Course course) {
        String sql = "update course set coursename=?,teaid=? where courseid=?";
        Object[] params = {course.getCourseName(),course.getTeaId(),course.getCourseId()};
        int row = 0;
        try {
            row = DBHelper.update(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }

    @Override
    public int delete(String id) {
        String sql = "delete from course where courseid=?";
        Object[] params = {id};
        int row = 0;
        try {
            row = DBHelper.update(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }

    @Override
    public int deleteBatch(String[] id) {
        int row=0 ;
        Connection conn=DBHelper.getConnection();
        String sql = "delete from course where courseid=?";
        try {
            PreparedStatement pre=conn.prepareStatement(sql);
            for(int i=0;i<id.length;i++){
                pre.setString(1,id[i]);
                //添加参数到批处理
                pre.addBatch();
            }
            //执行批处理
            int [] rows= pre.executeBatch();
            //统计删除行数
            for (int i = 0; i <rows.length ; i++) {
                row+=rows[i];
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return row;
    }

    @Override
    public Course findById(String id) {
        Connection conn = DBHelper.getConnection();
        Course course = null;
        String sql = "select * from course where courseid=?";
        try {
            PreparedStatement pre = conn.prepareStatement(sql);
            pre.setString(1, id);
            ResultSet rs = pre.executeQuery();
            if (rs.next()) {
                course = new Course();
                course.setCourseId(rs.getString("courseid"));
                course.setCourseName(rs.getString("coursename"));
                course.setTeaId(rs.getString("teaid"));

            }
            rs.close();
            pre.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }


        return course;
    }

    @Override
    public List<Course> findAll() {
        Connection conn = DBHelper.getConnection();
        List<Course> list = new ArrayList<Course>();
        Course course = null;
        String sql = "select * from course order by courseid asc ";
        try {
            PreparedStatement pre = conn.prepareStatement(sql);
            ResultSet rs = pre.executeQuery();
            while (rs.next()) {
                course= new Course();
                course.setCourseId(rs.getString("courseid"));
                course.setCourseName(rs.getString("coursename"));
                course.setTeaId(rs.getString("teaid"));
                list.add(course);

            }
            rs.close();
            pre.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }

    @Override
    public List<Course> findSearch(String keyword) {
        Connection conn = DBHelper.getConnection();
        List<Course> list = new ArrayList<Course>();
        Course course = null;
        String sql = "select * from course where coursename like '%\"+keyword+\"%' order by courseid asc ";
        try {
            PreparedStatement pre = conn.prepareStatement(sql);
            ResultSet rs = pre.executeQuery();
            while (rs.next()) {
                course= new Course();
                course.setCourseId(rs.getString("courseid"));
                course.setCourseName(rs.getString("coursename"));
                course.setTeaId(rs.getString("teaid"));
                list.add(course);

            }
            rs.close();
            pre.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }
}
